package jmine.tec.rpc.common.serialization.json;

import java.lang.reflect.Array;

import jmine.tec.json.JSON;
import jmine.tec.json.JSONObject;
import jmine.tec.json.JSONString;

/**
 * Conversor de JSON para Java e vice-versa
 * 
 * @author lundberg
 */
public class JSONArrayClassConverter extends AbstractJSONObjectConverter<Class<?>> implements JSONConverterStrategy {

    /**
     * {@inheritDoc}
     */
    public boolean matchesJava(Object object) {
        return object instanceof Class && ((Class) object).isArray();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected JSON objectType() {
        return new JSONString("arrayclass");
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected Class<?> instantiate(JSONConversionContext context, JSON json) {
        Class<?> componentType = (Class<?>) context.toJava(json.getProperty("value"));
        return Array.newInstance(componentType, 0).getClass();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected void fillObject(JSONConversionContext context, JSON json, Class<?> object) {
        // OK
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected void fillJSON(JSONConversionContext context, Object object, JSONObject json) {
        json.setProperty("value", context.toJSON(((Class) object).getComponentType()));
    }
}
